package com.ruoyi.campadmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.campadmin.mapper.CaVenueRentalPhotoMapper;
import com.ruoyi.campadmin.service.ICaVenueRentalPhotoService;
import com.ruoyi.campadmin.domain.CaVenueRentalPhoto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.campadmin.domain.CaVenueRental;
import com.ruoyi.campadmin.service.ICaVenueRentalService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CaVenueRentalPhotoServiceImpl implements ICaVenueRentalPhotoService {


    @Resource
    private CaVenueRentalPhotoMapper caVenueRentalPhotoMapper;
    
    
    @Resource
    private ICaVenueRentalService caVenueRentalService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param caVenueRentalPhoto 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<CaVenueRentalPhoto> getCaVenueRentalPhotoPage(PageDomain pageDomain, CaVenueRentalPhoto caVenueRentalPhoto , CaVenueRental caVenueRental) {
        if (Objects.isNull(caVenueRentalPhoto)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<CaVenueRentalPhoto> queryWrapper = new LambdaQueryWrapper<>(new CaVenueRentalPhoto());
            //if(!Objects.isNull(caVenueRentalPhoto.getName())){
            //	queryWrapper.like(CaVenueRentalPhoto::getName,caVenueRentalPhoto.getName());
            //}
            

            if (!Objects.isNull(caVenueRentalPhoto.getId())) {
                queryWrapper.eq(CaVenueRentalPhoto::getId, caVenueRentalPhoto.getId());
            }

            if (!Objects.isNull(caVenueRentalPhoto.getCaVenueRentalId())) {
                queryWrapper.eq(CaVenueRentalPhoto::getCaVenueRentalId, caVenueRentalPhoto.getCaVenueRentalId());
            }

            caVenueRental.setBaseEntityNull();
            caVenueRental.setId(null);
            if (!PageDomain.checkObjAllFieldsIsNull(caVenueRental)) {
                List<Long> idList = caVenueRentalService.getIdListByCaVenueRental(caVenueRental);
                queryWrapper.in(CaVenueRentalPhoto::getCaVenueRentalId, idList);
            }


            if (!Objects.isNull(caVenueRentalPhoto.getPhotoUrl())) {
                queryWrapper.like(CaVenueRentalPhoto::getPhotoUrl, caVenueRentalPhoto.getPhotoUrl());
            }

            if (!Objects.isNull(caVenueRentalPhoto.getStatus())) {
                queryWrapper.like(CaVenueRentalPhoto::getStatus, caVenueRentalPhoto.getStatus());
            }

            if (!Objects.isNull(caVenueRentalPhoto.getDelFlag())) {
                queryWrapper.like(CaVenueRentalPhoto::getDelFlag, caVenueRentalPhoto.getDelFlag());
            }

            if (!Objects.isNull(caVenueRentalPhoto.getCreateBy())) {
                queryWrapper.like(CaVenueRentalPhoto::getCreateBy, caVenueRentalPhoto.getCreateBy());
            }

            if (!Objects.isNull(caVenueRentalPhoto.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(caVenueRentalPhoto.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(caVenueRentalPhoto.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CaVenueRentalPhoto::getCreateTime, caVenueRentalPhoto.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(caVenueRentalPhoto.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CaVenueRentalPhoto::getCreateTime, caVenueRentalPhoto.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(caVenueRentalPhoto.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(caVenueRentalPhoto.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CaVenueRentalPhoto::getCreateTime, caVenueRentalPhoto.getParams().get("CreateTimeBeginTime"), caVenueRentalPhoto.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caVenueRentalPhoto.getUpdateBy())) {
                queryWrapper.like(CaVenueRentalPhoto::getUpdateBy, caVenueRentalPhoto.getUpdateBy());
            }

            if (!Objects.isNull(caVenueRentalPhoto.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(caVenueRentalPhoto.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(caVenueRentalPhoto.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CaVenueRentalPhoto::getUpdateTime, caVenueRentalPhoto.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(caVenueRentalPhoto.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CaVenueRentalPhoto::getUpdateTime, caVenueRentalPhoto.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(caVenueRentalPhoto.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(caVenueRentalPhoto.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CaVenueRentalPhoto::getUpdateTime, caVenueRentalPhoto.getParams().get("UpdateTimeBeginTime"), caVenueRentalPhoto.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caVenueRentalPhoto.getRemark())) {
                queryWrapper.like(CaVenueRentalPhoto::getRemark, caVenueRentalPhoto.getRemark());
            }
        Page<CaVenueRentalPhoto> caVenueRentalPhotoList= caVenueRentalPhotoMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(CaVenueRentalPhoto record:caVenueRentalPhotoList.getRecords()){
            record.setCaVenueRental(caVenueRentalService.getCaVenueRentalById(record.getCaVenueRentalId()));
        }

            return caVenueRentalPhotoList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public CaVenueRentalPhoto getCaVenueRentalPhotoById(Long id) {
        if (Objects.isNull(id)) {
            return new CaVenueRentalPhoto();
        }
        try {
            CaVenueRentalPhoto caVenueRentalPhoto = caVenueRentalPhotoMapper.selectById(id);
            caVenueRentalPhoto.setCaVenueRental(caVenueRentalService.getCaVenueRentalById(caVenueRentalPhoto.getCaVenueRentalId()));
            if (Objects.isNull(caVenueRentalPhoto)){
                caVenueRentalPhoto = new CaVenueRentalPhoto();
            }
            return caVenueRentalPhoto;

        } catch (Exception e) {
            e.printStackTrace();
            return new CaVenueRentalPhoto();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param caVenueRentalPhoto 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByCaVenueRentalPhoto(CaVenueRentalPhoto caVenueRentalPhoto) {
        if (Objects.isNull(caVenueRentalPhoto)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<CaVenueRentalPhoto> queryWrapper = new LambdaQueryWrapper<>(new CaVenueRentalPhoto());
            

            if (!Objects.isNull(caVenueRentalPhoto.getId())) {
                queryWrapper.eq(CaVenueRentalPhoto::getId, caVenueRentalPhoto.getId());
            }

            if (!Objects.isNull(caVenueRentalPhoto.getCaVenueRentalId())) {
                queryWrapper.eq(CaVenueRentalPhoto::getCaVenueRentalId, caVenueRentalPhoto.getCaVenueRentalId());
            }

            if (!Objects.isNull(caVenueRentalPhoto.getPhotoUrl())) {
                queryWrapper.like(CaVenueRentalPhoto::getPhotoUrl, caVenueRentalPhoto.getPhotoUrl());
            }

            if (!Objects.isNull(caVenueRentalPhoto.getStatus())) {
                queryWrapper.like(CaVenueRentalPhoto::getStatus, caVenueRentalPhoto.getStatus());
            }

            if (!Objects.isNull(caVenueRentalPhoto.getDelFlag())) {
                queryWrapper.like(CaVenueRentalPhoto::getDelFlag, caVenueRentalPhoto.getDelFlag());
            }

            if (!Objects.isNull(caVenueRentalPhoto.getCreateBy())) {
                queryWrapper.like(CaVenueRentalPhoto::getCreateBy, caVenueRentalPhoto.getCreateBy());
            }

            if (!Objects.isNull(caVenueRentalPhoto.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(caVenueRentalPhoto.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(caVenueRentalPhoto.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CaVenueRentalPhoto::getCreateTime, caVenueRentalPhoto.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(caVenueRentalPhoto.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CaVenueRentalPhoto::getCreateTime, caVenueRentalPhoto.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(caVenueRentalPhoto.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(caVenueRentalPhoto.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CaVenueRentalPhoto::getCreateTime, caVenueRentalPhoto.getParams().get("CreateTimeBeginTime"), caVenueRentalPhoto.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caVenueRentalPhoto.getUpdateBy())) {
                queryWrapper.like(CaVenueRentalPhoto::getUpdateBy, caVenueRentalPhoto.getUpdateBy());
            }

            if (!Objects.isNull(caVenueRentalPhoto.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(caVenueRentalPhoto.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(caVenueRentalPhoto.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CaVenueRentalPhoto::getUpdateTime, caVenueRentalPhoto.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(caVenueRentalPhoto.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CaVenueRentalPhoto::getUpdateTime, caVenueRentalPhoto.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(caVenueRentalPhoto.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(caVenueRentalPhoto.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CaVenueRentalPhoto::getUpdateTime, caVenueRentalPhoto.getParams().get("UpdateTimeBeginTime"), caVenueRentalPhoto.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caVenueRentalPhoto.getRemark())) {
                queryWrapper.like(CaVenueRentalPhoto::getRemark, caVenueRentalPhoto.getRemark());
            }

            List<CaVenueRentalPhoto> caVenueRentalPhotos = caVenueRentalPhotoMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CaVenueRentalPhoto caVenueRentalPhotoNo:caVenueRentalPhotos){
                idList.add(caVenueRentalPhotoNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过caVenueRental外键idList查询idList
     *
     * @param caVenueRentalIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByCaVenueRentalIdList(List<Long> caVenueRentalIdList) {
        if (Objects.isNull(caVenueRentalIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<CaVenueRentalPhoto> queryWrapper = new LambdaQueryWrapper<>(new CaVenueRentalPhoto());
            queryWrapper.in(CaVenueRentalPhoto::getCaVenueRentalId, caVenueRentalIdList);
            List<CaVenueRentalPhoto> caVenueRentalPhotos = caVenueRentalPhotoMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CaVenueRentalPhoto caVenueRentalPhoto:caVenueRentalPhotos){
                idList.add(caVenueRentalPhoto.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param caVenueRentalPhoto 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertCaVenueRentalPhoto(CaVenueRentalPhoto caVenueRentalPhoto) {
        if (Objects.isNull(caVenueRentalPhoto)) {
            return -1;
        }
        try {
        
            //caVenueRentalPhoto.setSpell(PinYin.getPinYin(caVenueRentalPhoto.getName()));
            return caVenueRentalPhotoMapper.insert(caVenueRentalPhoto);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param caVenueRentalPhoto 实体类
     * @return 结果
     */
    @Override
    public int updateCaVenueRentalPhotoById(CaVenueRentalPhoto caVenueRentalPhoto) {
        if (Objects.isNull(caVenueRentalPhoto)) {
            return -1;
        }
        try {
            //caVenueRentalPhoto.setSpell(PinYin.getPinYin(caVenueRentalPhoto.getName()));
            return caVenueRentalPhotoMapper.updateById(caVenueRentalPhoto);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteCaVenueRentalPhotoByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return caVenueRentalPhotoMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<CaVenueRentalPhoto> getCaVenueRentalPhotoList() {
        try {
            LambdaQueryWrapper<CaVenueRentalPhoto> queryWrapper = new LambdaQueryWrapper<>(new CaVenueRentalPhoto());
            queryWrapper.isNotNull(CaVenueRentalPhoto::getId);
            return caVenueRentalPhotoMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
